package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.modular.system.entity.BtyArchives;
import cn.stylefeng.guns.sys.modular.system.service.BtyArchivesService;
import cn.stylefeng.guns.sys.modular.system.service.BtyBxdaService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * 识别所属银行
 */
@Controller
@RequestMapping("/alipay")
public class AlipayController extends BaseController {

    private String PREFIX = "/modular/system/alipay/";

    @Autowired
    private BtyArchivesService archivesService;
    @Autowired
    private BtyBxdaService btyBxdaService;


    /**
     * 跳转到校验银行卡号页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "alipay.html";
    }


    /**
     * 跳转到修改列表
     */
    @RequestMapping("/edit")
    public String noticeUpdate(@RequestParam("id") Long id, ModelMap mapper) {
        if (ToolUtil.isEmpty(id)) {
            throw new RequestEmptyException();
        }
        //缓存区域修改前详细信息
        BtyArchives btyArchives = archivesService.getById(id);
        mapper.put("btyArchives",btyArchives);
        LogObjectHolder.me().set(btyArchives);
        return PREFIX + "edit.html";
    }


    /**
     * 修改列表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(BtyArchives btyArchives) {
        if (ToolUtil.isOneEmpty(btyArchives, btyArchives.getId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        String yhk = btyArchives.getYhk();//银行卡号

        //通过银行卡号获取银行简称
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=" + yhk + "&cardBinCheck=true";
        String content = restTemplate.getForObject(url, String.class);
        com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(content);

        String bank;
        String cardName;
        if(ToolUtil.isNotEmpty(jsonObject.get("bank"))){
            //银行简称
            bank = jsonObject.get("bank").toString();
            btyArchives.setBank(bank);

            //银行名称
            cardName = archivesService.cardUtil().get(bank);
            btyArchives.setCardName(cardName);

            btyArchives.setYhkStatus("1");//正常账号

            //通过银行简称获取行号
            String code = btyBxdaService.getBankCode(bank);
            btyArchives.setHh(code);//银行行号
            //行号为空
            if (ToolUtil.isEmpty(code)){
                btyArchives.setYhkStatus("2");//异常账号
            }

            archivesService.updateById(btyArchives);
            //手动修改行号
        }else if (ToolUtil.isNotEmpty(btyArchives.getHh())){
            //根据传过来的行号搜索对应银行
            Map<String, String> map = archivesService.getBank(btyArchives.getHh());

            if (ToolUtil.isNotEmpty(map)){
                btyArchives.setBank(map.get("bank"));
                btyArchives.setCardName(map.get("cardName"));
            }

            btyArchives.setYhkStatus("1");//正常账号
            archivesService.updateById(btyArchives);
        } else {

            btyArchives.setYhkStatus("2");//异常账号
            cardName = "该银行卡号不正确!";

            archivesService.updateById(btyArchives);

        }

        return SUCCESS_TIP;
    }


    /**
     * 获取通知列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String xm, String tel, String dm, String yhkStatus) {

        Page<Map<String, Object>> list = this.archivesService.listYhk(xm, tel, dm, yhkStatus);
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 导出
     * @param yhkStatus
     */
    @RequestMapping(value = "/export")
    @ResponseBody
    public List<Map> export(String yhkStatus){
        return this.archivesService.export(yhkStatus);
    }




//    public Object list(String xm, String tel, String dm, String yhkStatus) {
//
//        List<BtyArchives> list = this.archivesService.listYhk(xm, tel, dm, yhkStatus);
//        String cardName = "";
//
//        List arrayList = new ArrayList();//全部数据
//        List normalList = new ArrayList();//正常账号
//        List abnormalList = new ArrayList();//异常行号
//
//        for (BtyArchives btyArchives : list) {
//            String bankNo = btyArchives.getYhk();
//            //阿里银行卡接口
//            String url = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo="+bankNo+"&cardBinCheck=true";
//            RestTemplate restTemplate = new RestTemplate();
//            String result = restTemplate.getForObject(url, String.class);
//
//            // 转为 Json 对象
//            JSONObject json = new JSONObject(result);
//            //输入银行卡有效
//            if (String.valueOf(json.get("validated")) == "true"){
//                //获取到所属行简称
//                String bank = String.valueOf(json.get("bank"));
//                //银行名称
//                cardName = cardUtil().get(bank);
//
//                btyArchives.setYhkStatus("正常账号");//正常账号
//                btyArchives.setCardName(cardName);
//                normalList.add(btyArchives);
//
//            }else {
//
//                btyArchives.setYhkStatus("异常账号");//异常账号
//                cardName = "该银行卡号不正确!";
//
//                abnormalList.add(btyArchives);
//            }
//
//            arrayList.add(btyArchives);
//
//        }
//
//        LayuiPageInfo result = new LayuiPageInfo();
//        result.setData(arrayList);
////        result.setData(list);
//        result.setCount(20);
//
//        LayuiPageInfo normalResult = new LayuiPageInfo();
//        normalResult.setData(normalList);
//        normalResult.setCount(20);
//
//        LayuiPageInfo abnormalResult = new LayuiPageInfo();
//        abnormalResult.setData(abnormalList);
//        abnormalResult.setCount(20);
//
//        if (yhkStatus == null || yhkStatus == ""){
//            return result;
//        }else if (yhkStatus.equals("1")){
//            return normalResult;
//        }else {
//            return abnormalResult;
//        }
//
//    }


    @GetMapping("bankStatus")
    @ResponseBody
    public String bankStatus() throws Exception{

        //获取所有银行卡
        List<String> bankNoList = archivesService.getYhk();

        String cardName = "";
        for (String bankNo: bankNoList) {
            //银行代码请求接口 url
            //阿里银行卡接口
            String url = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo="+bankNo+"&cardBinCheck=true";
            RestTemplate restTemplate = new RestTemplate();
            String result = restTemplate.getForObject(url, String.class);

            // 转为 Json 对象
            JSONObject json = new JSONObject(result);
            BtyArchives btyArchives = new BtyArchives();

            //输入银行卡有效
            if (String.valueOf(json.get("validated")) == "true"){
                //获取到所属行简称
                String bank = String.valueOf(json.get("bank"));

                //银行名称
                cardName = archivesService.cardUtil().get(bank);

                btyArchives.setYhkStatus("0");//正常账号
                btyArchives.setCardName(cardName);

            }else {

                btyArchives.setYhkStatus("1");//异常账号

                cardName = "该银行卡号不正确!";
            }

        }

        return cardName;
    }




}
